<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;
use think\Controller;

/**
 * Description of Login
 *
 * @author Rung-PC
 */
class Login extends Controller{
    //put your code here
    public function index(){
        return $this->fetch();
    }
    
    public function login(){
        if(request()->isAjax()){
            $name = input('name');
            $pwd = input('pwd');            //halt($pwd);
            
            $result = db('users')->where('user_name',$name)->find();
            if(empty($result)){
                $data['user_name'] = $name;
                $data['passwd'] = $pwd;
                $data['nick_name'] = 'youke_'.uniqid();
                $data['head_img'] = 'default.jpg';
                $user_id = db('users')->insertGetId($data);
                if($user_id){
                    session('user_id',$user_id);
                    return ['status'=>1,'url'=>url('index/index/index',"fromid=$user_id&toid=1")];
                }else{
                    return ['status'=>0,'msg'=>'注册失败!'];
                }
            }else{
                $user_id = db('users')->where(['user_name'=>$name,'passwd'=>$pwd])->value('id');
                $toid = $user_id == 1 ? 2:1;
                if(!empty($user_id)){
                     session('user_id',$user_id);
                     return ['status'=>1,'url'=>url('index/index/index',"fromid=$user_id&toid=$toid")];
                }else{
                      return ['status'=>0,'msg'=>'密码错误!'];
                }
            }
        }
    }
}
